import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Gao zhao xu
 * Date: 2023-09-27
 * Time: 19:57
 */
public class MyLinkedList {
    static class ListNode {
        public int value;
        public ListNode next;
        public ListNode prev;

        public ListNode(int value) {
            this.value = value;
        }
    }

    public ListNode head;
    public ListNode last;

    //1.得到链表的长度
    public int size() {
        ListNode cur = head;
        int count = 0;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    //2.打印链表
    public void display() {
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.value + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    //3.查找链表中是否含有key
    public boolean contains(int key) {
        if (head == null) {
            return false;
        }
        ListNode cur = head;
        while (cur != null) {
            if (cur.value == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //4.头插法：
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        if (head == null) {
            last = node;
            head = node;
            return;
        }
        node.next = head;
        head.prev = node;
        head = node;
    }

    //5.尾插法：
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if (head == null) {
            last = node;
            head = node;
            return;
        }
        node.prev = last;
        last.next = node;
        last = node;
    }

    //6.任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index, int data) {
        if (index < 0 || index > size()) {
            throw new IndexOutBoundsException("获取数据时，位置不合法");
        }
        if (index == 0) {
            addFirst(data);
            return;
        }
        if (index == size()) {
            addLast(data);
            return;
        }
        ListNode node = new ListNode(data);
        ListNode cur = head;
        for (int i = 0; i < index - 1; i++) {
            cur = cur.next;
        }
        node.next = cur.next;
        node.prev = cur;
        cur.next.prev = node;
        cur.next = node;
    }

    //7.删除第一次出现关键字为key的节点
    public void remove(int key) {
        if (head == null) {
            return;
        }
        ListNode cur = head;
        while (cur != null) {
            if (cur.value == key) {
                if (cur == head) {
                    if (cur.next == null) {
                        head = null;
                    } else {
                        head = head.next;
                        head.prev = null;
                    }
                    return;
                }    else if (cur == last) {
                    last = last.prev;
                    last.next = null;
                    return;
                } else {
                    cur.prev.next = cur.next;
                    cur.next.prev = cur.prev;
                    return;
                }
            }
            cur = cur.next;
        }
    }
    //8.删除所有出现关键字为key的节点
    public void removeAllKey(int key) {
        if (head == null) {
            return;
        }
        ListNode cur = head;
        while (cur != null) {
            if (cur.value == key) {
                if (cur == head) {
                    if (cur.next == null) {
                        head = null;
                    } else {
                        head = head.next;
                        head.prev = null;
                    }
                } else if (cur == last) {
                    last = last.prev;
                    last.next = null;
                } else {
                    cur.prev.next = cur.next;
                    cur.next.prev = cur.prev;
                }
            }
            cur = cur.next;
        }
    }
    //9.清空链表
    public void clear() {
        //head = null;
        //last= null;
        ListNode cur = head.next;
        while (cur != null){
            head = null;
            head = cur;
            cur = cur.next;
        }
        head = null;
    }
}
